package edu.bbu.commonproject.backend.server.factory.thrift;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.logging.Level;

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TTransportException;

import edu.bbu.commonproject.backend.server.factory.SVRFactory;
import edu.bbu.commonproject.backend.server.services.CommunicationSRVServices;
import edu.bbu.commonproject.backend.server.services.thrift.ThriftCommunicationSRVServices;
import edu.bbu.commonproject.backend.server.services.thrift.communication.ServiceHandlers;
import edu.bbu.commonproject.backend.server.services.thrift.communication.SocketService;
import edu.bbu.commonproject.backend.server.services.thrift.communication.ThriftService;
import edu.bbu.commonproject.backend.utils.ServerLogger;
import edu.bbu.commonproject.backend.utils.SystemConfig;

/**
 * The Thrift server factory class.
 */
public class ThriftSRVFactory extends SVRFactory
{	
	/** The initialozation of THRIFT_PORT. */
	public static final int THRIFT_PORT = Integer.parseInt(SystemConfig.getValue(SystemConfig.THRIFT_PORT));
	
	/** The initialization of SOCKET_PORT. */
	public static final int SOCKET_PORT = Integer.parseInt(SystemConfig.getValue(SystemConfig.SOCKET_PORT));
	
	/** The servers thrift. */
	public static TServer thriftServer;
	
	/** The servers socket. */
	public static SocketService socketServer;
	
	/** The thrift communication SRV service. */
	private ThriftCommunicationSRVServices thriftCommunicationSRVServices;
	
	/**
	 * The constructor.
	 */
	public ThriftSRVFactory()
	{
		try
		{
			ServiceHandlers thriftServiceHandler = new ServiceHandlers();
			
			TServerSocket serverTransport = new TServerSocket(THRIFT_PORT);
			ThriftService.Processor serverProcessor = new ThriftService.Processor(thriftServiceHandler);
			thriftServer = new TThreadPoolServer(new TThreadPoolServer.Args(serverTransport).processor(serverProcessor));
			
			socketServer = new SocketService(SOCKET_PORT, thriftServiceHandler);
			
			thriftCommunicationSRVServices = new ThriftCommunicationSRVServices();
			
		}
		catch (TTransportException | IOException e)
		{
			ServerLogger.log(Level.SEVERE, "Failed to initialize the Thrift communication.");
		}
		
	}
	
	/**
	 * Getting the Thrift server services.
	 */
	public CommunicationSRVServices getCommunicationSRVServices()
	{
		return thriftCommunicationSRVServices;
	}
}
